/**
 * 
 */
package com.thinkgsp.jxc.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgsp.jxc.model.RpContactBalance;
import com.thinkgsp.jxc.model.RpCustomerContact;
import com.thinkgsp.jxc.persistence.ContactBalanceMapper;
import com.thinkgsp.jxc.persistence.mybatis.PagingResult;
import com.thinkgsp.jxc.service.ContactBalanceService;
import com.thinkgsp.util.PeriodUtil;

/**
 * 往来余额业务实现类
 * @author evan
 *
 */
@Service("cbService")
@Transactional
public class ContactBalanceServiceImpl extends GeneralServiceImpl implements
		ContactBalanceService {

	@Autowired
	private ContactBalanceMapper cbMapper;
	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.ContactBalanceService#getContactBalance(java.lang.Long)
	 */
	public RpContactBalance getContactBalance(Long id) {
		return cbMapper.getContactBalance(id);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.ContactBalanceService#getContactBalanceListForAccountsReceivable(java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<RpContactBalance> getContactBalanceListForAccountsReceivable(
			Integer begYear, Integer begPeriod, Integer endYear,
			Integer endPeriod) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		return cbMapper.getContactBalanceList(1, begin, end);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.ContactBalanceService#getContactBalanceListForAccountsReceivable(java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<RpContactBalance> getContactBalanceListForAccountsReceivable(
			Integer begYear, Integer begPeriod, Integer endYear,
			Integer endPeriod, Integer custId) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		return cbMapper.getContactBalanceListByCust(1, begin, end, custId);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.ContactBalanceService#getContactBalanceListForAccountsPayable(java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<RpContactBalance> getContactBalanceListForAccountsPayable(
			Integer begYear, Integer begPeriod, Integer endYear,
			Integer endPeriod) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		return cbMapper.getContactBalanceList(-1, begin, end);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.ContactBalanceService#getContactBalanceListForAccountsPayable(java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<RpContactBalance> getContactBalanceListForAccountsPayable(
			Integer begYear, Integer begPeriod, Integer endYear,
			Integer endPeriod, Integer custId) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		return cbMapper.getContactBalanceListByCust(-1, begin, end, custId);
	}

	public void setContactBalanceMapper(ContactBalanceMapper cbMapper) {
		this.cbMapper = cbMapper;
	}

	public List<RpCustomerContact> getCustomerContactList(Integer begYear,
			Integer begPeriod, Integer endYear, Integer endPeriod,Integer custId) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		return cbMapper.findCustomerContact(begin, end,custId);
	}

	public PagingResult<RpCustomerContact> getCustomerContactList(
			Integer begYear, Integer begPeriod, Integer endYear,
			Integer endPeriod, Integer custId, int start, int size) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("begPeriod", begin);
		params.put("endPeriod", end);
		if(custId != null) {
			params.put("custId", custId);
		}
		RowBounds rows = new RowBounds(start, size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.ContactBalanceMapper.findCustomerContactCount", 
				"com.thinkgsp.jxc.persistence.ContactBalanceMapper.findCustomerContactPaging", 
				params, rows);
	}
	
	public PagingResult<RpContactBalance> getContactBalanceListForAccountsReceivable(
			Integer begYear, Integer begPeriod, Integer endYear,
			Integer endPeriod, int start, int size) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", 1);
		params.put("begPeriod", begin);
		params.put("endPeriod", end);
		RowBounds rows = new RowBounds(start, size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.ContactBalanceMapper.getContactBalanceListCount", 
				"com.thinkgsp.jxc.persistence.ContactBalanceMapper.getContactBalanceListPaging", 
				params, rows);
	}

	public PagingResult<RpContactBalance> getContactBalanceListForAccountsPayable(
			Integer begYear, Integer begPeriod, Integer endYear,
			Integer endPeriod, int start, int size) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("direct", -1);
		params.put("begPeriod", begin);
		params.put("endPeriod", end);
		RowBounds rows = new RowBounds(start, size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.ContactBalanceMapper.getContactBalanceListCount", 
				"com.thinkgsp.jxc.persistence.ContactBalanceMapper.getContactBalanceListPaging", 
				params, rows);
	}

	public PagingResult<RpCustomerContact> getCustomerContactList(
			Integer begYear, Integer begPeriod, Integer endYear,
			Integer endPeriod, int start, int size) {
		BigDecimal begin = PeriodUtil.convertYearPeriodToDecimal(begYear, begPeriod);
		BigDecimal end = PeriodUtil.convertYearPeriodToDecimal(endYear, endPeriod);
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("begPeriod", begin);
		params.put("endPeriod", end);
		RowBounds rows = new RowBounds(start, size);
		return queryPagingHandler.queryPagingResult(
				"com.thinkgsp.jxc.persistence.ContactBalanceMapper.findCustomerContactCount", 
				"com.thinkgsp.jxc.persistence.ContactBalanceMapper.findCustomerContactPaging", 
				params, rows);
	}	

}
